<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    let accounts =[["John","johnsmith@mail.com","john_newyork@mail.com"],["John","johnsmith@mail.com","john00@mail.com"],["Mary","mary@mail.com"],["John","johnnybravo@mail.com"]]
    var accountsMerge = function (accounts) {
      let list = []
      //当accounts[i][0]name相等的时候 不一定是同一个账户 但是不相等的时候一定不是同一个账户
      for (let i = 0; i < accounts.length; i++) {
        for (let j = 0; j < accounts.length; j++) {
          // name一样
          if (accounts[i][0] == accounts[j][0]) {
            let OFFON = true
            for (let ei = 1; ei < accounts[i].length; ei++) {
              //其中有一个相等
              if (accounts[j].indexOf(accounts[i][ei]) > -1) {
                if (i == j) {
                  OFFON = false
                  continue
                }
                let xx = Array.from(new Set(accounts[i].concat(accounts[j])))
                let name = xx.splice(0, 1)
                xx.sort().unshift(name[0]);
                list.push(xx)
                accounts.splice(j,1)
                j--
                OFFON = false
                break;
              }
            }
            if (OFFON) {
              list.push(accounts[j].sort())
              accounts.splice(j, 1)
              j--
            }
          }
          else {
            console.log('名字不一样');
            list.push(accounts[j].sort())
            accounts.splice(j, 1)
            j--
          }
        }
      }
      return list
    }
    console.log('accountsMerge(accounts)', accountsMerge(accounts));
    let xx = [
    ["Kevin", "Kevin3@m.co", "Kevin5@m.co", "Kevin0@m.co"],
    ["Ethan", "Ethan5@m.co", "Ethan4@m.co", "Ethan0@m.co"],
    ["Hanzo", "Hanzo3@m.co", "Hanzo1@m.co", "Hanzo0@m.co"],
    ["Fern", "Fern5@m.co", "Fern1@m.co", "Fern0@m.co"]]

    let ii = [
    ["Ethan", "Ethan0@m.co", "Ethan4@m.co", "Ethan5@m.co"], 
    ["Gabe", "Gabe0@m.co", "Gabe1@m.co", "Gabe3@m.co"], 
    ["Hanzo", "Hanzo0@m.co", "Hanzo1@m.co", "Hanzo3@m.co"], 
    ["Kevin", "Kevin0@m.co", "Kevin3@m.co", "Kevin5@m.co"], 
    ["Fern", "Fern0@m.co", "Fern1@m.co", "Fern5@m.co"]]
  </script>

</body>

</html>